     #include "record.h"
attribute_value& attribute_value::operator=(const attribute_value& a)
{
	if(type_no == 0 && char_count != 0)
		delete [] char_value;
	if(a.type_no == 0)
	{
		char_value = new char[a.char_count];
		for(int i = 0; i < a.char_count; i++)
			char_value[i] = a.char_value[i];
		char_count = a.char_count;
		type_no = 0;
	}
	else
	{
		if(a.type_no == 1)
		{
			type_no = 1;
			float_value = a.float_value;
		}
		else
		{
			type_no = 2;
			int_value = a.int_value;
		}
	}
	return *this;
}

attribute_value::attribute_value(const attribute_value& a)
{
	if(a.type_no == 0)
	{
		char_value = new char[a.char_count];
		for(int i = 0; i < a.char_count; i++)
			char_value[i] = a.char_value[i];
		char_count = a.char_count;
		type_no = 0;
		int_value = 0;
		float_value = 0.0;
	}
	else
	{
		if(a.type_no == 1)
		{
			type_no = 1;
			float_value = a.float_value;
			int_value = 0;
			char_value = NULL;
			char_count = 0;
		}
		else
		{
			type_no = 2;
			int_value = a.int_value;
			float_value = 0.0;
			char_value = NULL;
			char_count = 0;
		}
	}
}


bool attribute_value::statisfy(int operation, const attribute_value& a)
{
	if(a.type_no != type_no) 
		return 0;
	else
	{			
		bool compare_result[6];
		if(a.type_no == 0)		//0-char,1-float,2-int
		{	
			char* tmp= new char[a.char_count+1];
			for(int i=0;i<a.char_count;i++)
				tmp[i]=a.char_value[i];
			tmp[a.char_count]='\0';
			CString temp(tmp);
			compare_result[0] = (char_value == temp);
			compare_result[1] = (char_value < temp);
			compare_result[2] = (char_value > temp);
			compare_result[3] = (char_value <= temp);
			compare_result[4] = (char_value >= temp);
			compare_result[5] = (char_value != temp);
		}
		if(a.type_no == 1)
		{
			float temp = a.float_value;
			compare_result[0] = (float_value == temp);
			compare_result[1] = (float_value < temp);
			compare_result[2] = (float_value > temp);
			compare_result[3] = (float_value <= temp);
			compare_result[4] = (float_value >= temp);
			compare_result[5] = (float_value != temp);
		}
		if(a.type_no == 2)
		{
			int temp = a.int_value;
			compare_result[0] = (int_value == temp);
			compare_result[1] = (int_value < temp);
			compare_result[2] = (int_value > temp);
			compare_result[3] = (int_value <= temp);
			compare_result[4] = (int_value >= temp);
			compare_result[5] = (int_value != temp);
		}
		return compare_result[operation];
	}
}


record::record(const record& a)
{
	attribute_count = a.attribute_count;
	for(int i = 0; i < a.attribute_count; i++)
		value[i] = a.value[i];
	position = a.position;
	previous = a.previous;
	next = a.next;
}

record::record(char* record_pointer, table_info* record_info)
{
	attribute_count=record_info->attribute_count;
	for(int i=0;i<attribute_count;i++)
	{
		if((record_info->info[i]).type=="int")
		{
			value[i].int_value=*((int *)record_pointer);
			value[i].type_no=2;
			record_pointer+=intScale;
		}
		else if ((record_info->info[i]).type=="char")
		{
			value[i].char_value= new char[(record_info->info[i]).type_count];
			value[i].char_count=(record_info->info[i]).type_count;
			char *p = value[i].char_value;
			for(int j = 0; j < (record_info->info[i]).type_count; j++)
				p[j] = record_pointer[j];
			record_pointer+=(record_info->info[i]).type_count;
			value[i].type_no=0;
		}
		else if ((record_info->info[i]).type=="float")
		{
			value[i].float_value=*((float *)record_pointer);
			value[i].type_no=1;
			record_pointer+=floatScale;
		}		
	}
	position.block_number=*((int *)record_pointer);
	record_pointer+=intScale;
	position.offset=*((int *)record_pointer);
	record_pointer+=intScale;
	previous.block_number=*((int *)record_pointer);
	record_pointer+=intScale;
	previous.offset=*((int *)record_pointer);
	record_pointer+=intScale;
	next.block_number=*((int *)record_pointer);
	record_pointer+=intScale;
	next.offset=*((int *)record_pointer);
	record_pointer+=intScale;
}

record& record::operator=(const record& a)
{
	attribute_count = a.attribute_count;
	for(int i = 0; i < a.attribute_count; i++)
		value[i] = a.value[i];
	position = a.position;
	previous = a.previous;
	next = a.next;
	return *this;
}

char* record::transformRecord(int& size)
{
	size=0;
	int record_num;
	for (record_num=0;record_num<attribute_count;record_num++)
	{
		if (value[record_num].type_no==0)
		{
			size+=charScale*value[record_num].char_count;	
		}
		else if(value[record_num].type_no==1)
		{
			size+=floatScale;
		}
		else
		{
			size+=intScale;
		}
		
	}
	size=size+3*address::getSize();
	char* record_array=new char[size];
	char* record_pointer=record_array;
	for (record_num=0;record_num<attribute_count;record_num++)
	{
		if (value[record_num].type_no==0)
		{
			for (int tempi=0;tempi<value[record_num].char_count;tempi++,record_array++)
			{
				*record_array=value[record_num].char_value[tempi];
			}
		}
		else if (value[record_num].type_no==1)
		{
			*((float *)record_array)=value[record_num].float_value;
			record_array+=floatScale;
		}
		else if (value[record_num].type_no==2)
		{
			*((int *)record_array)=value[record_num].int_value;
			record_array+=intScale;
		}
	}
	*((int *)record_array)=position.block_number;
	record_array+=intScale;
	*((int *)record_array)=position.offset;
	record_array+=intScale;
	*((int *)record_array)=previous.block_number;
	record_array+=intScale;
	*((int *)record_array)=previous.offset;
	record_array+=intScale;
	*((int *)record_array)=next.block_number;
	record_array+=intScale;
	*((int *)record_array)=next.offset;
	record_array+=intScale;
	return  record_pointer;
}
